SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CheckIsNullOrEmpty]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[CheckIsNullOrEmpty]
GO
 
CREATE FUNCTION [dbo].[CheckIsNullOrEmpty] 
(
	-- Add the parameters for the function here
	@text nvarchar(300)
)
RETURNS bit
AS
BEGIN
	-- Declare the return variable here
	DECLARE @IsNullOrEmpty bit

	IF @text = NULL OR @text = ''
		BEGIN
			SELECT @IsNullOrEmpty = 0
		END
	ELSE  
		BEGIN
			SELECT @IsNullOrEmpty  = 1
		END

	-- Return the result of the function
	RETURN @IsNullOrEmpty

END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetPersonName]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GetPersonName]
GO
 
CREATE FUNCTION [dbo].[GetPersonName] 
(
	-- Add the parameters for the function here
	@title     nvarchar(5)
   ,@firstName nvarchar(50)
   ,@lastName  nvarchar(50)
)
RETURNS nvarchar(110)
AS
BEGIN
	-- Declare the return variable here
	DECLARE @personName nvarchar(110)

	SELECT @personName = LTRIM(ISNULL(@title, '') + ' ' + @firstName + ' ' + @lastName)	

	-- Return the result of the function
	RETURN @personName
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetModeName]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GetModeName]
GO
 
CREATE FUNCTION [dbo].[GetModeName] 
(
	-- Add the parameters for the function here
	@transactionModeID int
)
RETURNS nvarchar(20)
AS
BEGIN
	-- Declare the return variable here
	DECLARE @transactionModeName nvarchar(20)
	
	SELECT @transactionModeName = TransactionModeName FROM [dbo].[TransactionModes] t 
	WHERE t.TransactionModeID = @transactionModeID
	
	-- Return the result of the function
	RETURN @transactionModeName
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetPostalModeName]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GetPostalModeName]
GO
 
CREATE FUNCTION [dbo].[GetPostalModeName] 
(
	-- Add the parameters for the function here
	@postalModeID int
)
RETURNS nvarchar(20)
AS
BEGIN
	-- Declare the return variable here
	DECLARE @postalModeName nvarchar(20)
	
	SELECT @postalModeName = PostalModeName FROM [dbo].[PostalModes] p 
	WHERE p.postalModeID = @postalModeID
	
	-- Return the result of the function
	RETURN @postalModeName
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetDonationPlaceName]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GetDonationPlaceName]
GO
 
CREATE FUNCTION [dbo].[GetDonationPlaceName] 
(
	-- Add the parameters for the function here
	@donationPlaceID int
)
RETURNS nvarchar(20)
AS
BEGIN
	-- Declare the return variable here
	DECLARE @donationPlaceName nvarchar(20)
	
	SELECT @donationPlaceName = DonationPlaceName FROM [dbo].[DonationPlace] d 
	WHERE d.DonationPlaceID = @donationPlaceID
	
	-- Return the result of the function
	RETURN @donationPlaceName
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetDonationAccountName]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GetDonationAccountName]
GO
 
CREATE FUNCTION [dbo].[GetDonationAccountName] 
(
	-- Add the parameters for the function here
	@donationAccountID int
)
RETURNS nvarchar(20)
AS
BEGIN
	-- Declare the return variable here
	DECLARE @donationAccountName nvarchar(20)
	
	SELECT @donationAccountName = DonationAccountName FROM [dbo].[DonationAccounts] d 
	WHERE d.DonationAccountID = @donationAccountID
	
	-- Return the result of the function
	RETURN @donationAccountName
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetSearchString]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GetSearchString]
GO
 
CREATE FUNCTION [dbo].[GetSearchString] 
(
	-- Add the parameters for the function here
	@originalString nvarchar(50)
)
RETURNS     nvarchar(305)
AS
BEGIN
	-- Declare the return variable here
	DECLARE @searchString   nvarchar(305)

	SELECT @searchString  = '%' + @originalString + '%'

	-- Return the result of the function
	RETURN @searchString

END
GO